package git.bankview.wl.auth;

import java.util.Vector;
import java.sql.*;
import laya.semi.process.*;

import git.bankview.wl.bean.LogonBean;
import git.bankview.sl.report.ReportCommon;
import git.bankview.sl.db.*;
import git.bankview.wl.auth.UserAuth;

import org.apache.log4j.Logger;

public class UserReportSchdValidateActivity
	implements IGenericClassActivity
{
	private static Logger log = Logger.getLogger(UserReportSchdValidateActivity.class);
   
	public void execute(IProcessContext oCtx)
		throws Exception
	{
		// logon-name:schd:method-array
		String[] vArg = oCtx.getActivityParameter().split(":");
		LogonBean user = (LogonBean)oCtx.getField(vArg[0]);
		Object tSchd = oCtx.getField(vArg[1]);
		String[] vSchd = (tSchd.getClass().isArray() ? 
			(String[])tSchd : new String[] {String.valueOf(tSchd)});

		String[] vTransid = queryTransidsBySchds(vSchd);
		String[] vMethod = vArg[2].split(",");

		for (int i = 0; i < vTransid.length; ++i)
		{
			for (int j = 0; j < vMethod.length; ++j)
			{
				UserAuth.validate(user, vTransid[i], Integer.parseInt(vMethod[j]));
			}
		}
	}
	
	private String[] queryTransidsBySchds(String[] vSchd)
		throws Exception
	{
		StringBuffer oSql = new StringBuffer("select transid from dqschdentry where schdid in (");
		for (int i = 0; i < vSchd.length; ++i)
		{
			if (0 < i)
				oSql.append(", ?");
			else
				oSql.append("?");
		}
		oSql.append(") group by transid");
		log.debug(oSql);
		
		Connection oConn = DBConnectionFactory.getCoreConnection();
		try
		{
			PreparedStatement ps = oConn.prepareStatement(oSql.toString());
			for (int i = 0; i < vSchd.length; ++i)
			{
				ps.setString(i + 1, vSchd[i]);
			}
			ResultSet rs = ps.executeQuery();
			Vector v = new Vector();
			while(rs.next())
			{
				v.addElement(rs.getString(1).trim());
			}
			return (String[])v.toArray(new String[0]);
		}
		finally
		{
			oConn.close();
		}
	}
}
